
#include "gl_texture_mgr.h"
#include "gl_inc.h"
#include "bmp_loader.h"
#include "stdio.h"


GlTextureMgr::GlTextureMgr()
{


}

GlTextureMgr::~GlTextureMgr()
{

}


Texture *GlTextureMgr::LoadTexture(char *textureName)
{
	Texture *texture = NULL;
	char texN[100];
	sprintf(texN,"%s\%s",textureResourceDirectory,textureName);
	BmpData *imageData = BmpLoader(texN);
	if(imageData != NULL && imageData->data != NULL)
	{
		texture = new Texture(textureName);
		if(texture!= NULL)
		{
			glGenTextures(1, &texture->textureId);
			glBindTexture(GL_TEXTURE_2D, texture->textureId);

			glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, imageData->width, imageData->height, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData->data);

			//Specify Filtering
			//@TODO: 	should move outside the loop,
			//			as of now only bmp loading is there... so no probs
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering

			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

			BmpFree(imageData);
		}

	}

	return texture;
}

void GlTextureMgr::BindTexture(Texture *texture)
{
	if(texture != NULL)
		glBindTexture(GL_TEXTURE_2D, texture->textureId);
	else
		glBindTexture(GL_TEXTURE_2D, 0);
}


void GlTextureMgr::BindTexture(char *textureName)
{
	Texture *texture = GetTexture(textureName);
	BindTexture(texture);
}
